<?php

class AuthorController extends Controller
{
	/**
    * Set layout
   */
   public $layout = '//layouts/cms/main';
   
   public function actionIndex()
	{
		$page = Yii::app()->request->getParam('page',1); // Lấy số trang
      $limit = Yii::app()->request->getParam('limit',10); // Lấy số dòng hiển thị trên grid
      $start = $limit*$page - $limit; // Thứ tự bản ghi bắt đầu lấy  
	   $model = new Authors();  
      $list = $model->findAll(array('offset' => $start,
                                       'limit' => $limit));
      
      $total_record = $model->count();
      if(!is_numeric($page)) $page = 1;
      if(!is_numeric($limit)) $limit = 10;
      if( $total_record >0 ) {
      	$total_pages = ceil($total_record/$limit);
      } else {
      	$total_pages = 0;
      }
      
		$this->render('index',array('list' => $list,
                                  'limit' => $limit,
                                  'start' => $start,
                                  'page' => $page,
                                  'total_pages' => $total_pages,
                                  'total_record' => $total_record));
	}
   
   public function actionCreate(){
      $model = new Authors();
      if(isset($_POST['Authors'])){
         $model->attributes = $_POST['Authors'];
         if($model->validate()){
            if($model->save())
               Yii::app()->user->setFlash('success','Thêm mới thành công');
         }
      }
      $this->pageTitle = 'Thêm mới tác giả';
      $this->layout = '//layouts/cms/create';
      $this->render('create',array(
         'model' => $model,
      ));
   }
   
   public function actionEdit($id){
      $model = Authors::model()->findByPk($id);
      if(isset($_POST['Authors'])){
         $model->attributes = $_POST['Authors'];
         if($model->validate()){
            if($model->save())
               Yii::app()->user->setFlash('success','Thêm mới thành công');
         }
      }
      $this->pageTitle = 'Cập nhật thông tin tác giả';
      $this->layout = '//layouts/cms/create';
      $this->render('create',array(
         'model' => $model,
      ));
   }
   
   
   
   /**
    * Delete record
   */
	public function actionDelete(){
      $error = 1;
      $record_affected = 0;
      $list_id = Yii::app()->request->getParam('list_id',array());
      $session_user = Yii::app()->request->getParam('session_user','');
      if($session_user == Yii::app()->session['session_user'])
      {
         if(count($list_id) > 0){
            $sql = 'DELETE FROM authors WHERE aut_id IN(-1';
            foreach ($list_id as $item){
               if(is_numeric($item))
                  $sql .= ',' . $item;
            }
            $sql .= ')';
            $db = Yii::app()->db;
            $cmd = $db->createCommand($sql);
            $record_affected = $cmd->execute();
            if($record_affected >= 0){
               $error = 0;
            }else{
               $error = 1;
            }
         }
         
      }
      echo json_encode(array('error' => $error,
                             'record_affected' => $record_affected)); 
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}